package com.itheima.info.manager.controller;

import com.itheima.info.manager.domain.Student;
import com.itheima.info.manager.domain.Teacher;
import com.itheima.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private TeacherService teacherService = new TeacherService();
    private Scanner sc = new Scanner(System.in);
    private Teacher tea = new Teacher();

    public void start() {

        tealo:
        while (true) {

            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");

            int i = sc.nextInt();

            switch (i) {

                case 1:

                    addTeacher();
                    break;

                case 2:

                    deleteTeacher();
                    break;

                case 3:
                    updateTeacher();
                    break;

                case 4:

                    getAllTeacher();
                    break;

                case 5:

                    System.out.println("感谢使用教师管理系统");
                    break tealo;

                default:

                    System.out.println("输入有误，请重新输入！");

            }
        }
    }

    private void updateTeacher() {
        String updateId = inputTeacherId();
        Teacher newTea = inputTeacherInfo(updateId);
        teacherService.updateTeacher(updateId, newTea);

        System.out.println("修改成功!");
    }

    private void deleteTeacher() {
        String teaId = inputTeacherId();
        teacherService.deleteTeacherById(teaId);
        System.out.println("删除成功!");
    }

    private String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入教师id:");
            id = sc.next();
            boolean exists = teacherService.isExist(id);
            if (!exists) {
                System.out.println("您输入的id不存在, 请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }

    private void getAllTeacher() {
        Teacher[] teas = teacherService.findAllTeacher();

        if (teas == null) {
            System.out.println("暂无教师信息！");
            return;
        }

        System.out.println("工号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < teas.length; i++) {
            Teacher t = teas[i];
            if (t != null) {
                System.out.println(t.getId() + "\t\t" + t.getName() + "\t\t" + t.getAge() + "\t\t" + t.getBirthday());
            }
        }
    }


    private void addTeacher() {

        String teaId;
        while (true) {

            System.out.println("请输入教师工号：");
            teaId = sc.next();
            boolean flag = teacherService.isExist(teaId);

            if (flag) {
                System.out.println("输入的工号已存在，请重新输入！");
            } else {
                break;
            }

        }
        inputTeacherInfo(teaId);

        boolean result = teacherService.addTeacher(tea);

        if (result){
            System.out.println("添加成功！");
        }else {
            System.out.println("添加失败！");
        }

    }

    private Teacher inputTeacherInfo( String teaId) {
        System.out.println("请输入教师姓名：");
        String teaName = sc.next();
        System.out.println("请输入教师年龄：");
        String teaAge = sc.next();
        System.out.println("请输入教师生日：");
        String teaBirthday = sc.next();

        tea.setId(teaId);
        tea.setName(teaName);
        tea.setAge(teaAge);
        tea.setBirthday(teaBirthday);
        return tea;
    }
}
